#####
## Replication File:
##
## Who Supports QAnon? A Case 
## Study in Political Extremism
#####

################################################################################

####
## Load necessary packages and set
## working directory
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS Catalina 10.15.7)
####

#install.packages("stats") # version 3.6.3
#install.packages("car") # version 3.0-10
#install.packages("lattice") # version 0.20-38
#install.packages("latticeExtra") # version 0.6-29
#install.packages("foreign") # version 0.8-75

library(stats)
library(car)
library(lattice)
library(latticeExtra)
library(foreign)

# Set working directory

################################################################################

####
## Read in data
####

fl2018 <- read.dta("Clean Data, Florida 2018.dta")

july2019 <- read.dta("Clean Data, July 2019.dta")

march2020 <- read.dta("Clean Data, March 2020.dta")

june2020 <- read.dta("Clean Data, June 2020.dta")

fl2020 <- read.dta("Clean Data, Florida 2020.dta")

october2020 <- read.dta("Clean Data, October 2020.dta")

################################################################################

####
## Figure 1
####

means <- read.csv("Means Over Time.csv", header = TRUE)

plot1 <- xyplot(mean ~ year,
       data = means,
       aspect = 0.75,
       ylim = c(0, 50),
       xlab = " ",
       par.settings = list(clip = list(panel = FALSE)), 
       ylab = "Mean Thermometer Score",
       scales = list(x = list(at=c(1, 12, 20, 23, 27), 
                              label=c("08/18", "07/19",
                                      "03/20", "06/20", "10/20"))),
       key=list(columns=1, points=list(pch=c(1, 16)), 
                text=list(c("Florida", "National")),
                corner = c(0.1,0.9)),
       panel = function(x, y, ...){
         panel.xyplot(x, y, col = "black", type = "b", pch = c(1,16,16,16,1,16))
         panel.segments(means$year, means$lower, means$year, means$upper)
         panel.text(-4.75, 50, label = "A)", font = 2)
       }
       )

set.seed(1234)
plot2 <- xyplot(qanonft ~ ideo,
       data = march2020,
       aspect = 1,
       xlab = "Ideological Self-Identification",
       ylab = "QAnon Movement Thermometer",
       par.settings = list(clip = list(panel = FALSE)), 
       panel = function(x, y, ...){
         panel.xyplot(jitter(x), jitter(y), col="dark grey")
         #panel.lmline(x, y, col = "red")
         panel.smoother(x, y, col = "black")
         panel.text(-.81, 107, label = "B)", font = 2)
       }
       )

set.seed(1234)
plot3 <- xyplot(qanonft ~ pid,
       data = march2020,
       aspect = 1,
       xlab = "Partisanship",
       ylab = "QAnon Movement Thermometer",,
       par.settings = list(clip = list(panel = FALSE)), 
       panel = function(x, y, ...){
         panel.xyplot(jitter(x), jitter(y), col="dark grey")
         #panel.lmline(x, y, col = "red")
         panel.smoother(x, y, col = "black")
         panel.text(-.26, 107, label = "C)", font = 2)
       }
)

pdf("figure1.pdf", width = 8, height = 8)
print(plot1, position = c(0, .5, 1, 1), more = T)
print(plot2, position = c(0, 0, .5, .5), more = T)
print(plot3, position = c(.5, 0, 1, .5), more = F)
dev.off()

################################################################################

####
## Figure 2
####

myvars <- c("darktriad2", "falseinfo2", "violence2", "pid2",
            "ideo2", "conthink2", "q82")

stacked <- stack(march2020, select = c("triad2", "falseinfo2", "violence2", "pid2",
                                       "ideo2", "conthink2"))

plot4 <- xyplot(rep(0:100) ~ values | ind,
                data = stacked,
                aspect = 1,
                xlab = "",
                ylab = "Predicted Thermometer Score",
                ylim = c(11, 48),
                strip=strip.custom(factor.levels=c("Dark Triad", 
                                                   "Spread False Info.",  
                                                   "Accept Violence",
                                                   "Partisanship",
                                                   "Ideology",
                                                   "Conspiracy Thinking")),
                panel = function(x, y, ...){
                        panel.xyplot(x, y, col="white")
                        panel.rug(x, col="black")
                }
)

preds <- read.csv("Predictions.csv", header = TRUE)

plot5 <- xyplot(estimate ~ scale | as.factor(order),
       data = preds,
       aspect = 1,
       ylim = c(11, 48),
       col = c("black", "dark grey", "dark grey"),
       type = "l",
       lty = c(1, 2, 2),
       groups = group,
       xlab = "",
       ylab = "Predicted Thermometer Score",
       strip=strip.custom(factor.levels=c("Dark Triad", 
                                          "Spread False Info.",  
                                          "Accept Violence",
                                          "Partisanship",
                                          "Ideology",
                                          "Conspiracy Thinking")),
)

pdf("figure2.pdf", width = 6.5, height = 5)
plot4 + plot5
dev.off()

################################################################################

####
## Figure A1
####

pdf("figureA1-1.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = fl2018,
          xlab = "QAnon Movement Thermometer",
          main = "A. August 2018 (Florida)"
)
dev.off()

pdf("figureA1-2.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = july2019,
          xlab = "QAnon Movement Thermometer",
          main = "B. July 2019"
)
dev.off()

pdf("figureA1-3.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = march2020,
          xlab = "QAnon Movement Thermometer",
          main = "C. March 2020"
)
dev.off()

pdf("figureA1-4.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = june2020,
          xlab = "QAnon Movement Thermometer",
          main = "D. June 2020"
)
dev.off()

pdf("figureA1-5.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = fl2020,
          xlab = "QAnon Movement Thermometer",
          main = "E. June 2020 (Florida)"
)
dev.off()

pdf("figureA1-6.pdf", width = 5, height = 5)
histogram(~qanonft,
          aspect = 1,
          data = october2020,
          xlab = "QAnon Movement Thermometer",
          main = "F. October 2020"
)
dev.off()

################################################################################

####
## Figure A2
####

set.seed(1234)
therm1 <- xyplot(qanonft ~ diffpartytherm,
                 data = march2020,
                 aspect = 1,
                 xlab = "Difference in Party Thermometers",
                 ylab = "QAnon Movement Thermometer",
                 par.settings = list(clip = list(panel = FALSE)), 
                 panel = function(x, y, ...){
                         panel.xyplot(jitter(x), jitter(y), col="dark grey")
                         #panel.lmline(x, y, col = "red")
                         panel.smoother(x, y, col = "black")
                         panel.text(-138, 107, label = "A)", font = 2)
                 }
)

set.seed(1234)
therm2 <- xyplot(qanonft ~ diffcandtherm,
                 data = march2020,
                 aspect = 1,
                 xlab = "Difference in Candidate Thermometers",
                 par.settings = list(clip = list(panel = FALSE)), 
                 ylab = "QAnon Movement Thermometer",
                 panel = function(x, y, ...){
                         panel.xyplot(jitter(x), jitter(y), col="dark grey")
                         #panel.lmline(x, y, col = "red")
                         panel.smoother(x, y, col = "black")
                         panel.text(-138, 107, label = "B)", font = 2)
                 }
)

pdf("figureA2.pdf", width = 6.5, height = 5)
print(therm1, position = c(0, 0, .5, 1), more = TRUE)
print(therm2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

####
## Figure A3
####

preds2 <- read.csv("Predictions, IDEO.csv", header = TRUE)

pdf("figureA3.pdf", width = 6, height = 6)
xyplot(estimate ~ scale | item,
       data = preds2,
       aspect = 1,
       col = c("black", "dark grey", "dark grey"),
       type = "l",
       lty = c(1, 2, 2),
       groups = group,
       xlab = "",
       ylab = "Predicted Thermometer Score",
)
dev.off()


